<?php
class ProjectController extends Zend_Controller_Action
{
	protected $_auth;
	protected $_acl;
	protected $_userGroup;
	
	public function init()
	{
		$auth = Zend_Auth::getInstance();
		$userInfo = $auth->getStorage()->read();
		$groupBase = new GroupBase();
		if ($userInfo) {
			$userGroups = $groupBase->getUserGroup($userInfo->name);
		}
		$this->_userGroup = $userGroups[0]['name'] ? $userGroups[0]['name'] : "Users";
		$this->_acl = new Acl_PPMIS();
		$this->view->acl = $this->_acl;
		$this->view->userGroup = $this->_userGroup;
	}
	
	public function preDispatch()
	{
		$this->_auth = Zend_Auth::getInstance();
		if($this->_auth->getIdentity()) {
			$this->view->auth = "success";
			$userInfo = $this->_auth->getStorage()->read();
			$this->view->userInfo = $userInfo;
		}
		$Configuration = new Configuration();
		$configurationData = $Configuration->getConfigurationInfo();
		$this->view->configuration = $configurationData;
		//$this->view->current = 'project';
		//$this->view->auth = 'Success';
	}
	
	public function indexAction()
	{
		if($this->_auth->getIdentity()) {
			$this->view->current = 'admin';
		} else {
			$this->view->current = 'project';
		}
		
		$project  = new Project();
		$data = $project->fetchAll($project->select());
		$this->view->data = $data->toArray();
	}
	
	public function addAction()
	{
		if ($this->_acl->isAllowed($this->_userGroup,null, 'admin')) {	
			$this->view->current = 'admin';
			$project  = new Project();
			$form = new AddProjectForm();
			
			if ($this->getRequest()->isPost()) {
				$formData = $this->_request->getPost();
				if ($form->isValid($formData)) {
					//Zend_Debug::dump($formData);
					$projectData = array(
							'pname' => $formData['pname'],
							'pkey' => $formData['pkey'],
							'url' => $formData['url'],
							'lead' => $formData['lead'],
							'description' => $formData['description'],
					);
					$project->insert($projectData);
				}
			}
			
			$this->view->form = $form;
		} else {
			$this->_redirect('project/invalid');
		}
	}
	
	public function editAction()
	{
		$this->view->current = 'admin';
		$projectId = $this->_getParam('projectId');
		$form = new EditProjectForm();
		
		if ($this->getRequest()->isPost()) {
			$formData = $this->_request->getPost();
			if ($form->isValid($formData)) {
				Zend_Debug::dump($formData);
			}
		}
		$project = new Project();
		$projectData = $project->getAdapter()->fetchRow($project->select()->where('id = ?',$projectId));
		$form->populate($projectData);							
		$this->view->form = $form;
	}
	
	public function browseAction()
	{
		if ($this->_acl->isAllowed($this->_userGroup, null, "admin")) {	
			$this->view->current = "project";
			$projectId = $this->_getParam('project');
			$project  = new Project();
			$projectVersion = new ProjectVersion();
			$component = new Component();
			
			$projectData = $project->getAdapter()->fetchRow($project->select()->where('id = ?',$projectId));
			$versions = $projectVersion->getProjectVersion($projectId);
			$components = $component->getProjectComponents($projectId);
			//Zend_Debug::dump($components);
			$this->view->projectData= $projectData;
			$this->view->versions = $versions;
			$this->view->components = $components;
			$this->view->projectId = $projectId;
		} else {
			$pid = $this->_getParam('project');
			$this->_redirect("issue/openissues/pid/".$pid);
		}
	}
	
	public function roadmapAction()
	{
		$projectId = $this->_getParam('project');
		$project = new Project();
		$component = new Component();
		$issue  = new Issue();
		$projectData = $project->getAdapter()->fetchRow($project->select()->where('id',$projectId));
		//$data = $component->getAdapter()->fetchAll($component->select()->where('project = ?', $projectId));
		
		$projectSummary = $issue->getIssueSummaryData();												
		$openIssues = $issue->getOpenIssuesData();
		$totalIssues = $issue->numRows();
		$this->view->projectData = $projectData;
		$this->view->totalIssues = $totalIssues;
		$this->view->projectSummary = $projectSummary;
		$this->view->openIssues = $openIssues;
		//$this->view->components = $data;
		$this->view->projectId = $projectId;
	}
	
	public function invalidAction()
	{
		
	}
	
}